package com.shiva.oms.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.sf.shiva.oms.common.constant.LogTableConstant;
import com.shiva.oms.logview.api.view.MainFrame;
import com.shiva.oms.logview.domain.LogData;

/**
 * 
 * 描述：开启线程异步做分类计数，将执行完成后的结果显示到界面
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年5月6日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
public class LogDataClassificateIndex {

    // 初始化线程池，newFixedThreadPool适用于稳定的并发;
    private static ListeningExecutorService Service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
    private List<LogData> rawDatas;
    private MainFrame frameParent;
    
    public LogDataClassificateIndex(MainFrame frameParent, List<LogData> rawDatas) {
        this.frameParent = frameParent;
        this.rawDatas = rawDatas;
        this.frameParent.setClassIndexMap(null);
        execTask();
    }

    public void execTask() {
        ClassificateTask task = new ClassificateTask(this.rawDatas);
        ListenableFuture<Map<String, Object>> submit = Service.submit(task);
        Futures.addCallback(submit, new FutureCallback<Map<String, Object>>() {

                @Override
                public void onSuccess(Map<String, Object> classMap) {
                    frameParent.setLevelCheckBox(classMap);
                    frameParent.setClassIndexMap(classMap);
                }

                @Override
                public void onFailure(Throwable t) {
                    System.out.println("创建索引失败");
                // t.printStackTrace();
                }
            });
    }

    class ClassificateTask implements Callable<Map<String, Object>> {

        private List<LogData> rawDatas;

        public ClassificateTask(List<LogData> rawDatas) {
            this.rawDatas = rawDatas;
        }

        @Override
        public Map<String, Object> call() throws Exception {
            long startTime = System.currentTimeMillis(); // 获取开始时间
            Map<String, Object> categoryNames = new HashMap<>();
            if (rawDatas == null || rawDatas.isEmpty()) {
                return categoryNames;
            }
            rawDatas.stream().forEach(data -> setGroupIndex(categoryNames, data));
            long endTime = System.currentTimeMillis(); // 获取结束时间
            System.out.println("创建索引运行时间：" + (endTime - startTime) + "ms");
            return categoryNames;
        }

        private void setGroupIndex(Map<String, Object> categoryIndex, LogData data) {
            Object attrIndex = categoryIndex.get(LogTableConstant.LEVEL_ATTR);
            if (attrIndex == null) {
                attrIndex = ArrayListMultimap.create();
            }
            ((Multimap<String, LogData>) attrIndex).put(data.getLevel().toString(), data);
            categoryIndex.put(LogTableConstant.LEVEL_ATTR, attrIndex);
        }
    }
}
